﻿using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SS.Common.Utilities
{
    /// <summary>
    /// Class for implementing extension methods
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Within a specified input string, replaces all strings that match a specified
        /// regular expression with a specified replacement string.
        /// </summary>
        /// <param name="s">The string to search for a match.</param>
        /// <returns></returns>
        public static string StripTags(this string s)
        {
            return Regex.Replace(s, "<.*?>", string.Empty);
        }

        /// <summary>
        /// TrimStart with string parameter
        /// </summary>
        /// <param name="s"></param>
        /// <param name="trimChars"></param>
        /// <returns></returns>
        public static string TrimStart(this string s, string trimChars)
        {
            return s.TrimStart(trimChars.ToCharArray());
        }

        /// <summary>
        /// Truncate a string with specified length
        /// </summary>
        /// <param name="s"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Truncate(this string s, int length)
        {
            var result = s;
            if(s.Length > length)
            {
                result = "...";
                s = s.Substring(0, length);
                var lastPos = s.LastIndexOf(" ", System.StringComparison.Ordinal);
                if (lastPos != -1)
                {
                    result = s.Substring(0, lastPos) + "...";
                }
            }
            return result;
        }

        public static string ToUpperFirstCharacter(this string s)
        {
            return string.IsNullOrEmpty(s) ? s : s[0].ToString().ToUpper() + s.Substring(1);
        }

        public static string ToLowerFirstCharacter(this string s)
        {
            return string.IsNullOrEmpty(s) ? s : s[0].ToString().ToLower() + s.Substring(1);
        }

        /// <summary>
        /// Remove specified characters
        /// </summary>
        /// <param name="s"></param>
        /// <param name="chars"></param>
        /// <returns></returns>
        public static string RemoveInvalidChars(this string s, params char[] chars)
        {
            return chars.Aggregate(s, (current, c) => current.Replace(c.ToString(), string.Empty));
        }

        public static string ReplaceFontSize(this string s, int fontSize)
        {
            return Regex.Replace(s, "(font-size:)\\s?.\\dpx;", "font-size: " + fontSize + "px;");
        }

        public static string ReplaceFontFamily(this string s, string fontFamily)
        {
            return Regex.Replace(s, "(font-family:)\\s?.*?;?", fontFamily);
        }

        public static string RemoveUnicodeSign(this string s)
        {
            char[] arrChar = { 'a', 'A', 'd', 'D', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y' };
            char[][] uniChar = {
            new char[] {'á', 'à', 'ả', 'ã', 'ạ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ'},
            new char[] {'Á', 'À', 'Ả', 'Ã', 'Ạ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ă', 'Ặ', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ'},
            new char[] {'đ'},
            new char[] {'Đ'},
            new char[] {'é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ'},
            new char[] {'É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ'},
            new char[] {'í', 'ì', 'ỉ', 'ĩ', 'ị'},
            new char[] {'Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị'},
            new char[] {'ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ỡ', 'ợ'},
            new char[] {'Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ớ', 'Ờ', 'Ỡ', 'Ợ'},
            new char[] {'ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự'},
            new char[] {'Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự'},
            new char[] {'ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ'},
            new char[] {'Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ'}};

            for (int i = 0; i < uniChar.Length; i++)
            {
                for (int j = 0; j < uniChar[i].Length; j++)
                {
                    s = s.Replace(uniChar[i][j], arrChar[i]);
                }
            }
            return s;
        }

        /// <summary>
        /// Remove unicode characters 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string RemoveUnicodeChars(this string s)
        {
            for (int i = 32; i < 48; i++)
            {
                s = s.Replace(((char)i).ToString(), " ");
            }

            while (s.Contains("  "))
            {
                s = s.Replace("  ", " ");
            }

            s = s.Trim();

            s = s.Replace("–", "-");
            s = s.Replace(".", "-");
            s = s.Replace(" ", "-");
            s = s.Replace(",", "-");
            s = s.Replace(";", "-");
            s = s.Replace(":", "-");
            s = s.Replace("?", "-");
            s = s.Replace("!", "-");
            s = s.Replace("“", "-");
            s = s.Replace("”", "-");
            s = s.Replace("\t", "-");

            while (s.Contains("__"))
            {
                s = s.Replace("__", "-");
            }

            if (s[s.Length - 1].Equals('_'))
            {
                s = s.Substring(0, s.Length - 1);
            }

            //str9 = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str9);
            var regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            var strFormD = s.Normalize(System.Text.NormalizationForm.FormD);

            return regex.Replace(strFormD, string.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').ToLower();
        }

        public static string RemoveControlCharacters(this string s)
        {
            var newString = new StringBuilder();
            if (!string.IsNullOrEmpty(s))
            {
                foreach (var ch in s)
                {
                    if (!char.IsControl(ch))
                    {
                        newString.Append(ch);
                    }
                }
            }
            return newString.ToString();
        }
    }

}